<?php

/*
List of functions

activateTheme($id) - activates a particular theme
deleteTheme($id) - deletes a particular theme
getActivatedThemeID() - returns activated theme ID
getActivatedThemeAttrib() - returns the activated theme attrib as per param
getTheme($id=-1) - may get a single theme or the entire lot
saveTheme($name,$desc,$bannertop=false,$bannerbottom=false,$body=1) - saves theme with the param preferences


*/

class buildpress_engine {

	var $activatedTheme;
	var $allTheme;

	var $BPOptions;
	var $BPThemes;
	var $BPColors;

	function buildpress_engine() {
		$this->BPOptions = "buildpressOptions"; //initialize vars
		$this->BPThemes = "buildpressThemes"; //initialize vars
		$this->BPColors = "buildpressColors"; //initialize vars
	}


	function getColors($id = -1) {
		global $wpdb;

		$colorData = get_option($this->BPColors);

		if($id == -1)
			return $colorData;
		else
			return $colorData[$id];
	}

	function saveColors($body_bg, $hd_ft_bg, $hd_ft_text, $bd_bg, $a_link, $a_hover) {
		global $wpdb;

		$colorData = array($body_bg, $hd_ft_bg, $hd_ft_text, $bd_bg, $a_link, $a_hover);

		$existingColorData = $this->getColors(); //gets existing theme data

		$newColorData = $existingColorData; //store existing data in var

		$newColorData[] = $colorData; //add new color options var

		if ( update_option($this->BPColors, $newColorData) ) //update db with theme options
			return $lastkey = end( array_keys($newColorData) );
		else
			return false;
	}

	function deleteColor($id) {
		global $wpdb;

		$colorData = $this->getColors();

		unset($colorData[$id]);

		if ( update_option($this->BPColors, $colorData) )
			return 1;
		else
			return 0;
	}

	/*
		function activateTheme
		params :
		$id - int
		activates a theme
	*/
	function activateTheme($id) {
		global $wpdb;

		$optionsData = array('activated' => $id);

		if ( update_option($this->BPOptions, $optionsData) )
			return 1;
		else
			return 0;
	}


	/*
		function deleteTheme
		params :
		$id - int
		deletes a theme
	*/
	function deleteTheme($id) {
		global $wpdb;

		$themeData = $this->getTheme();

		unset($themeData[$id]);

		if ( update_option($this->BPThemes, $themeData) )
			return 1;
		else
			return 0;

	}

	/*
		function getActivatedThemeID
		params : none
		returns the activated themeID
	*/
	function getActivatedThemeID() {
		global $wpdb;

		$existingOptions = get_option($this->BPOptions); //gets existing options data

		return $existingOptions['activated']; //returns id of theme

	}


	/*
		function  getActivatedThemeAttrib
		params :
		$id - int
		$attrib - string
		returns the activated theme attrib as per param
	*/
	function getActivatedThemeAttrib($id, $attrib) {
		global $wpdb;

		$themeData = $this->getTheme($this->getActivatedThemeID());

		return $themeData[$attrib];
	}

	/*
		function getTheme
		params :
		$id = -1 - int
		either returns all the themes or just one
	*/
	function getTheme($id = -1) {
		global $wpdb;

		$themeData = get_option($this->BPThemes);

		if($id == -1)
			return $themeData;
		else
			return $themeData[$id];
	}


	function checkTheme($name,$id=true) {
		global $wpdb;

		$themeData = $this->getTheme();

			foreach ( $themeData as $k=>$v ) {
				if ( $name == $v['themename'] ) {
					if ( $id == true )
						return $k;
					else
						return true;

					break;
				}
				else continue;
			}
			return false;
	}

	/*
		function saveTheme
		params :
		$name - string
		$desc - string
		$bannertop=false - bool
		$bannerbottom=false - bool
		$body=1 - int
		$extracss= string
	*/
	function saveTheme($name,$desc,$colorscheme,$bannertop=false,$bannerbottom=false,$docstyle="doc",$body=1,$extracss,$ss) {
		global $wpdb;

		$themeOptions = array('themename' => $name,
							  'themedesc' => $desc,
							  'colorscheme' => $colorscheme,
							  'bannertop' => $bannertop,
							  'bannerbottom' => $bannerbottom,
							  'docstyle' => $docstyle,
							  'body' => $body,
							  'stylesheets' => $ss,
							  'extracss' => $extracss);

		$existingThemeData = $this->getTheme(); //gets existing theme data

		$newThemeData = $existingThemeData; //store existing data in var

		$newThemeData[] = $themeOptions; //add new theme options var

		if ( update_option($this->BPThemes, $newThemeData) ) //update db with theme options
			return true;
		else
			return false;
	}

	/*
		function editTheme
		params :
		$name - string
		$desc - string
		$bannertop=false - bool
		$bannerbottom=false - bool
		$body=1 - int
		$extracss= string
	*/
	function editTheme($id, $name, $desc, $colorscheme, $bannertop = false, $bannerbottom = false, $docstyle = "doc", $body = 1, $extracss, $ss) {
		global $wpdb;

		$themeOptions = array('themename' => $name,
							  'themedesc' => $desc,
							  'colorscheme' => $colorscheme,
							  'bannertop' => $bannertop,
							  'bannerbottom' => $bannerbottom,
							  'docstyle' => $docstyle,
							  'body' => $body,
							  'stylesheets' => $ss,
							  'extracss' => $extracss);

		$existingThemeData = $this->getTheme(); //gets existing theme data

		$existingThemeData[$id] = $themeOptions; //store existing data in var

		if ( update_option($this->BPThemes, $existingThemeData) ) //update db with theme options
			return true;
		else
			return false;
	}

}

$b = new buildpress_engine();
